package com.fwpsl.springframework;

import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.core.metrics.StartupStep;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @author: 风戏fw
 * @date: 2024/6/6
 * @description:
 */
class YootkApplicationStartup implements ApplicationStartup {
    private static final Logger LOGGER = Logger.getLogger(YootkApplicationStartup.class.getSimpleName());
    private static String name;// 保存启动步骤的名称
    private static long startTimestamp; // 启动步骤的触发时间戳
    private static final YootkStartupStep.YootkTags yootkTags = new YootkStartupStep.YootkTags();

    @Override
    public StartupStep start(String name) { // 步骤开始
        YootkApplicationStartup.name = name; // 保存步骤的名称
        YootkApplicationStartup.startTimestamp = System.currentTimeMillis(); // 保存步骤开始的时间戳
        LOGGER.info(String.format("【start】“{%s}”步骤开始执行。", YootkApplicationStartup.name)); // 日志的记录
        return new YootkStartupStep();
    }

    static class YootkStartupStep implements StartupStep { // 定义启动步骤
        @Override
        public String getName() {
            return YootkApplicationStartup.name; // 启动步骤的名称
        }

        @Override
        public long getId() {
            return 3; // 定义启动步骤的ID
        }

        @Override
        public Long getParentId() {
            return null;
        }

        @Override
        public StartupStep tag(String key, String value) {
            YootkApplicationStartup.yootkTags.put(key, new YootkTag(key, value)); // 保存处理标签
            return this;
        }

        @Override
        public StartupStep tag(String key, Supplier<String> value) {
            YootkApplicationStartup.yootkTags.put(key, new YootkTag(key, value.get()));
            return this;
        }

        @Override
        public Tags getTags() {
            return YootkApplicationStartup.yootkTags;
        }

        @Override
        public void end() {
            LOGGER.info(String.format("【end】“{%s}”步骤执行完毕，所耗费的时间为：{%d}",
                    YootkApplicationStartup.name,
                    (System.currentTimeMillis() - YootkApplicationStartup.startTimestamp)));
        }

        static class YootkTags extends LinkedHashMap<String, Tag>
                implements StartupStep.Tags {// 定义标签集合

            @Override
            public Iterator<Tag> iterator() {
                return this.values().stream().collect(Collectors.toList()).iterator();
            }
        }

        class YootkTag implements StartupStep.Tag {
            private String key;
            private String value;

            public YootkTag(String key, String value) {
                this.key = key;
                this.value = value;
            }

            @Override
            public String getKey() {
                return this.key;
            }

            @Override
            public String getValue() {
                return this.value;
            }
        }
    }
}